package com.ctgu.tengyun.bean.convert;

import com.ctgu.tengyun.bean.response.FaultSolutionListResponse;
import com.ctgu.tengyun.bean.response.ProductFaultSolutionInfo;
import com.ctgu.tengyun.domain.FaultSolution;
import com.ctgu.tengyun.domain.ProductInfo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by liaoweizhi on 2019/8/17.
 */
public class FaultSolutionConvert {

    public static FaultSolutionListResponse toFaultSolutionListResponse(Integer totalCount,
                                                                        Integer pageSize,
                                                                        List<FaultSolution> faultSolutionList,
                                                                        Map<String, ProductInfo> productNoMap) {
        FaultSolutionListResponse response = new FaultSolutionListResponse();

        response.setTotalCount(totalCount);
        response.setTotalPage((int) Math.ceil(totalCount / (double) pageSize));

        List<ProductFaultSolutionInfo> productFaultSolutionInfoList = new ArrayList<>(faultSolutionList.size());
        faultSolutionList.forEach(item -> {
            productFaultSolutionInfoList.add(toProductFaultSolutionInfo(item, productNoMap.get(item.getRefId())));
        });
        response.setFaultSolutionList(productFaultSolutionInfoList);
        return response;

    }

    public static ProductFaultSolutionInfo toProductFaultSolutionInfo(FaultSolution faultSolution, ProductInfo productInfo) {
        ProductFaultSolutionInfo productFaultSolutionInfo = new ProductFaultSolutionInfo();
        BeanUtils.copyProperties(faultSolution, productFaultSolutionInfo);
        productFaultSolutionInfo.setProductNo(faultSolution.getRefId());
        productFaultSolutionInfo.setProductName(null != productInfo ? productInfo.getName() : "");
        productFaultSolutionInfo.setVideoFile(FileConvert.fillFileBeanUrlPath(faultSolution.getVideoFile()));

        return productFaultSolutionInfo;
    }
}
